package com.example.allshengmedical.system.activity;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.example.allshengmedical.R;
import com.example.allshengmedical.base.BaseActivity;
import com.example.allshengmedical.mainmenu.activity.MainMenuActivity;
import com.example.allshengmedical.system.presenter.LEDAreaPresenter;
import com.example.allshengmedical.system.view.ILEDAreaView;
import com.example.allshengmedical.util.ByAlert;
import com.itboye.allshengmedical.sci.DefaultSerialControl;
import com.itboye.allshengmedical.sci.ISerialControl;
import com.itboye.allshengmedical.sci.cmd.req.CmdType;
import com.itboye.allshengmedical.sci.packet.resp.BaseRespPacket;

public class LEDAreaActivity extends BaseActivity implements ILEDAreaView {
	LEDAreaPresenter ledAreaPresenter;

	ToggleButton LED1_switch, LED2_switch, LED3_switch, LED4_switch;
	ISerialControl controlInstance;

	@Override
	protected int setcontent() {
		// TODO Auto-generated method stub
		return R.layout.activity_ledarea;
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		controlInstance = DefaultSerialControl.getInstance();
		controlInstance.init();
		controlInstance.start();
		controlInstance.setHandler(handler);
		switchChange();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		ledAreaPresenter = new LEDAreaPresenter(this);
		ledAreaPresenter.initAppHead();
	}

	@Override
	public void initAppHead() {
		// TODO Auto-generated method stub

		setSystemTime();
		setActionBar();
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ISerialControl.FindPacketMessage:
				updateMsg(msg.getData());
				break;
			case ISerialControl.SCTimeoutMessage:
				break;
			}
		}

	};

	private void switchChange() {
		LED1_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					LED2_switch.setChecked(false);
					LED3_switch.setChecked(false);
					LED4_switch.setChecked(false);
					controlInstance.addCommand(CmdType.OpenLight, null, 1);
				} else {
					controlInstance.addCommand(CmdType.CloseLight, null, 1);
				}
			}
		});
		LED2_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					LED1_switch.setChecked(false);
					LED3_switch.setChecked(false);
					LED4_switch.setChecked(false);
					controlInstance.addCommand(CmdType.OpenLight, null, 2);
				} else {
					controlInstance.addCommand(CmdType.CloseLight, null, 2);
				}
			}
		});
		LED3_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					LED2_switch.setChecked(false);
					LED1_switch.setChecked(false);
					LED4_switch.setChecked(false);
					controlInstance.addCommand(CmdType.OpenLight, null, 3);
				} else {
					controlInstance.addCommand(CmdType.CloseLight, null, 3);
				}
			}
		});
		LED4_switch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
				if (arg1) {
					LED2_switch.setChecked(false);
					LED3_switch.setChecked(false);
					LED1_switch.setChecked(false);
					controlInstance.addCommand(CmdType.OpenLight, null, 4);
				} else {
					controlInstance.addCommand(CmdType.CloseLight, null, 4);
				}
			}
		});

	}

	private void updateMsg(Bundle msg) {
		// TODO Auto-generated method stub
		String cmdType = msg.getString("cmdType");
		BaseRespPacket packet = (BaseRespPacket) msg.getSerializable("packet");
		if (cmdType.length() < 1)
			return;
		// 根据type 转换packet 为你想要的
		CmdType type = CmdType.getCmdType(cmdType.substring(1));
		// 发送指令后这里会收到数据
		Log.d("read_data", "接受到的响应数据包是" + cmdType + type.toDesc());
		switch (type) {
		case AreaCalibration:
			break;
		}
	}

	private void setActionBar() {
		// TODO Auto-generated method stub
		Button leftfirstBtn;
		Button leftsecond_btn;
		TextView title;
		leftfirstBtn = BindView(R.id.leftfirst_btn);
		leftsecond_btn = BindView(R.id.leftsecond_btn);
		title = BindView(R.id.title);
		leftfirstBtn.setVisibility(View.VISIBLE);
		leftsecond_btn.setVisibility(View.VISIBLE);
		leftsecond_btn.setText(R.string.item_actionbar_home);
		leftfirstBtn.setText(R.string.item_actionbar_back);
		title.setText("LED设置");
		leftsecond_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(LEDAreaActivity.this,
						MainMenuActivity.class);
				startActivity(intent);
				finish();
			}
		});
		leftfirstBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(LEDAreaActivity.this,
						OpticalActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	private void setSystemTime() {
		// TODO Auto-generated method stub
		TextView date, fileName;
		date = BindView(R.id.date);
		fileName = BindView(R.id.file_name);

		// 获取系统时间
		SimpleDateFormat sdfDate = new SimpleDateFormat("MM dd,yyyy");
		date.setText(sdfDate.format(new Date()));

		// 文件路径置空
		fileName.setText("");

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		controlInstance.addCommand(CmdType.CloseLight, null, 0);
		SystemClock.sleep(1000);
		controlInstance.pause();
	}
}
